package com.badlogic.androidgames.jumper;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.gl.Camera2D;
import com.badlogic.androidgames.framework.gl.SpriteBatcher;
import com.badlogic.androidgames.framework.impl.GLScreen;
import com.badlogic.androidgames.framework.math.OverlapTester;
import com.badlogic.androidgames.framework.math.Rectangle;
import com.badlogic.androidgames.framework.math.Vector2;


public class MainMenuScreen extends GLScreen {

	Camera2D guiCam;
	SpriteBatcher batcher;
	Vector2 touchPoint; /*Since we use a Camera2D,we also need a Vector2 instance to 
	transform the touch coordinates to world coordinates*/
	Rectangle soundBound;
	Rectangle playBounds;
	Rectangle highscoresBounds;
	Rectangle helpBounds; 
	
	public MainMenuScreen(Game game) {
		super(game);
		// TODO Auto-generated constructor stub
		guiCam = new Camera2D(glGraphics, 320, 480);
		batcher = new SpriteBatcher(glGraphics, 100);
		soundBound = new Rectangle(0, 0, 64, 64);
		playBounds = new Rectangle(160 - 150, 200 + 18, 300, 36);
		highscoresBounds = new Rectangle(160 - 150, 200 - 18, 300, 36);
		helpBounds = new Rectangle(160 - 150, 200 - 18 - 36, 300, 36);
		touchPoint = new Vector2();
	}

	@Override
	public void update(float deltaTime) {
		// TODO Auto-generated method stub
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();
		int len =  touchEvents.size();
		for(int i = 0; i < len; i++){
			TouchEvent event = touchEvents.get(i);
			if(event.type == TouchEvent.TOUCH_UP){
				touchPoint.set(event.x, event.y);
				guiCam.touchToWorld(touchPoint);
				if(OverlapTester.pointInRectangle(playBounds, touchPoint)){
					Assets.playSound(Assets.clickSound);
					game.setScreen(new GameScreen(game));
					return;		
				}
				if(OverlapTester.pointInRectangle(highscoresBounds, touchPoint)){
					Assets.playSound(Assets.clickSound);
					game.setScreen(new HighscoreScreen(game));
					return;		
				}
				if(OverlapTester.pointInRectangle(helpBounds, touchPoint)){
					Assets.playSound(Assets.clickSound);
					game.setScreen(new HelpScreen(game));
					return;
				}
				if(OverlapTester.pointInRectangle(soundBound, touchPoint)){
					Assets.playSound(Assets.clickSound);
					Settings.soundEnabled = !Settings.soundEnabled;
					if(Settings.soundEnabled)
						Assets.music.play();
					else
						Assets.music.pause();
				}
			}
		}

	}

	@Override
	public void present(float deltaTime) {
		// TODO Auto-generated method stub
		 GL10 gl = glGraphics.getGl();
		 gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
         guiCam.setViewportAndMatrices();
         
         gl.glEnable(GL10.GL_TEXTURE_2D);
         batcher.beginBatch(Assets.background);
         batcher.drawSprite(160, 240, 320, 480, Assets.backgroundRegion);
         batcher.endBatch();
         
         gl.glEnable(GL10.GL_BLEND);
		 gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		 batcher.beginBatch(Assets.items);
		 batcher.drawSprite(160, 480 - 10 - 71, 274, 142, Assets.logo);
		 batcher.drawSprite(160, 200, 300, 110, Assets.mainMenu);
		 batcher.drawSprite(32, 32, 64, 64, Settings.soundEnabled?Assets.soundOn:Assets.soundOff);
		 batcher.endBatch();
		 
		 gl.glDisable(GL10.GL_BLEND);
		 
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		Settings.save(game.getFileIO());

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
